import './Login.css';
import { Button, Checkbox, Form, Input } from 'antd';
import { useNavigate, useSearchParams } from 'react-router-dom';
import { encryptBySM2, demoPublicKey } from '@/utils/smCrypto'

function Login() {
    const navigate = useNavigate()
    const [searchParams] = useSearchParams();
    const redirect = searchParams.get('redirect');
    const prevPath = localStorage.getItem('prevPath');
    // 获取回跳地址
    const redirectPath = new URLSearchParams(location.search).get('redirect') || '/home';

    const onFinish = async (values) => {

        console.log('Success:', values, prevPath, redirectPath);
        if (values.username && values.password) {
            // 1. 加密密码

            // 在登录时使用
            const publicKey = await getPublicKey()

            const encryptedPassword = encryptBySM2(values.password, demoPublicKey)
            console.log('加密后的密码', encryptedPassword)

            localStorage.setItem('accessToken', encryptedPassword);

            navigate(redirectPath || '/home?age=24', {
                state: {
                    name: 'zs',
                },
                // 则不可回退
                replace: true
            })
            // location.reload();
        }


    };
    // 在加密前先获取公钥
    const getPublicKey = async () => {
        // const response = await axios.get('/api/getPublicKey')
        // return response.data.publicKey
        return '00000000000'
    }

    const onFinishFailed = (errorInfo) => {
        console.log('Failed:', errorInfo);
    };
    return (
        <div className="container">
            <div className="login-box">
                <div className='login-div'>
                    <h2>系统入口</h2>
                    <Form
                        size='large'
                        className='login-form'
                        name="basic"
                        labelCol={{ span: 6 }}
                        wrapperCol={{ span: 18 }}
                        initialValues={{ remember: true }}
                        onFinish={onFinish}
                        onFinishFailed={onFinishFailed}
                        autoComplete="off"
                    >
                        <Form.Item
                            label="Username"
                            name="username"
                            rules={[{ required: true, message: 'Please input your username!' }]}
                        >
                            <Input />
                        </Form.Item>

                        <Form.Item
                            label="Password"
                            name="password"
                            rules={[{ required: true, message: 'Please input your password!' }]}
                        >
                            <Input.Password />
                        </Form.Item>

                        {/* <Form.Item name="remember" valuePropName="checked" label={null}>
                            <Checkbox>Remember me</Checkbox>
                        </Form.Item> */}

                        <Form.Item label={null}>
                            <Button type="primary" htmlType="submit">
                                Submit
                            </Button>
                        </Form.Item>
                    </Form>
                </div>
            </div>
        </div>

    )
}

export default Login